﻿using System;
using System.Linq;
using CvModel;
using DAL;

namespace LAB3_Cv
{
    public partial class NewAd : System.Web.UI.Page
    {
        private Admin _currentAdmin;
        private CvModel.WorkAd _deletedWorkAd;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurrentAdmin"]!= null)
            {
                _currentAdmin = Session["CurrentAdmin"] as Admin;
            }
            if (Session["DeletedAd"] != null)
            {
                int deletedWorkAdId = int.Parse(Session["DeletedAd"].ToString());
            _deletedWorkAd = WorkAdDB.GetWorkAd(deletedWorkAdId);
            }
            
            if (!IsPostBack)
            {
                if (Session["CurrentAdmin"] != null)
                {
                    GetContactPersonsToDropDownList();
                    if (Session["DeletedAd"] != null)
                    {
                        

                        TextBoxCity.Text = _deletedWorkAd.City;
                        TextBoxDuration.Text = _deletedWorkAd.Duration;
                        TextBoxProfession.Text = _deletedWorkAd.Proffession;
                        TextBoxQualifications.Text = _deletedWorkAd.Qualifications;
                        TextBoxSalary.Text = _deletedWorkAd.Salary;
                        TextBoxWorkDescription.Text = _deletedWorkAd.WorkDescription;
                        CalendarApplicationDay.SelectedDate = _deletedWorkAd.DayOfApplication;
                        DropDownListContactPerson.SelectedValue = _deletedWorkAd.ContactPersonContactPersonId.ToString();
                    }
                }
            }
        }

        private void GetContactPersonsToDropDownList()
        {
            
            var contactPersons = ContactPersonDB.GetAllContactPersonFromWorkAdminId(_currentAdmin.AdminId);
            var query =
                (from c in contactPersons
                 select new { Name = c.FirstName + " " + c.LastName, Id = c.ContactPersonId }).ToList();

            DropDownListContactPerson.DataSource = query;
            DropDownListContactPerson.DataTextField = "Name";
            DropDownListContactPerson.DataValueField = "Id";
            DropDownListContactPerson.DataBind();
        }

        protected void ButtonCreateAd_Click(object sender, EventArgs e)
        {
            try
            {
                var workAd = new CvModel.WorkAd()
                {
                    City = TextBoxCity.Text,
                    DateAdded = DateTime.Now,
                    Proffession = TextBoxProfession.Text,
                    DayOfApplication = CalendarApplicationDay.SelectedDate,
                    WorkDescription = TextBoxWorkDescription.Text,
                    Qualifications = TextBoxQualifications.Text,
                    Duration = TextBoxDuration.Text,
                    Salary = TextBoxSalary.Text,
                    IsDeleted = false,
                    AdminId = _currentAdmin.AdminId,
                    ContactPersonContactPersonId = int.Parse(DropDownListContactPerson.SelectedValue)
                };

                if (Session["DeletedAd"] != null)
                {
                    WorkAdDB.UpdateWorkAd(workAd, _deletedWorkAd.WorkAdId);
                    Session["DeletedAd"] = null;
                    _deletedWorkAd = null;
                }
                else
                {
                    WorkAdDB.CreateWorkAd(workAd);
                }
                Response.Redirect("~/WorkAdmin.aspx");
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}